package com.gz.mylife.app;

import java.util.List;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.util.ToastUtils;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.OfflineDialog;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;
import com.zl.smartmall.library.ISmartMall;
import com.zl.smartmall.library.listener.GetInvoiceTypeListener;
import com.zl.smartmall.library.po.InvoiceTypeInfo;
import com.zl.smartmall.library.util.Tools;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SetupInvoice extends BaseActivity implements OnClickListener, OnLoadingCoverRefreshListener {
	
	private GetInvoiceTypeListener mGetInvoiceTypeListener;
	private LayoutInflater mInflater;
	
	private EditText mInvoiceHead;
	private LinearLayout mTypeLayout;
	private LoadingCover mLoadingCover;
	private OfflineDialog mOfflineDialog;
	private TextView mCurrentSelectDetail;
	private TextView mCurrentSelectType;
	private TextView mNotTrouble;
	private TextView mNormalInvoice;
	private Button mOk;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup_invoice);

		mInflater = getLayoutInflater();
		initListener();
		
//		mActionBar.setTitle("设置发票信息");
		setActionbarTitle("设置发票信息", false);
		mLoadingCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mLoadingCover.setOnLoadingCoverRefreshListener(this);
		mTypeLayout = (LinearLayout) findViewById(R.id.layout_invoice_type);
		mInvoiceHead = (EditText) findViewById(R.id.et_invoice_head);
		mOk = (Button) findViewById(R.id.btn_ok);
		mOk.setOnClickListener(this);
		mNotTrouble = (TextView) findViewById(R.id.tv_not_trouble);
		mNotTrouble.setOnClickListener(this);
		mNormalInvoice = (TextView) findViewById(R.id.tv_normal_invoice);
		mNormalInvoice.setOnClickListener(this);
		mOfflineDialog = (OfflineDialog) mInflater.inflate(R.layout.offline_dialog, null);
		mCurrentSelectType = mNotTrouble;
		
		onLoadingCoverRefresh();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.setup_invoice, menu);
		return true;
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
//		Tools.recordBehavious(getApplicationContext(), TAG, Define.BEHAVIOUS_PAUSE);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
//		Tools.recordBehavious(getApplicationContext(), TAG, Define.BEHAVIOUS_RESUME);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_invoice_type:
			if(mCurrentSelectDetail != v) {
				mCurrentSelectDetail.setCompoundDrawablePadding(Tools.dip2px(getApplicationContext(), 3));
				mCurrentSelectDetail.setCompoundDrawables(getDrawable(R.drawable.ic_shopping_unselected), 
						null, null, null);
				((TextView) v).setCompoundDrawablePadding(Tools.dip2px(getApplicationContext(), 3));
				((TextView) v).setCompoundDrawables(getDrawable(R.drawable.ic_shopping_selected), 
						null, null, null);
				mCurrentSelectDetail = (TextView) v;
			}
			break;
			
		case R.id.btn_ok:
			if(mCurrentSelectType == mNotTrouble) {
				finish();
				
			}else if(TextUtils.isEmpty(mInvoiceHead.getText().toString())) {
				ToastUtils.showToast(this, "请输入发票抬头");
			
			}else {
				Intent data = new Intent();
				data.putExtra("invoiceHead", mInvoiceHead.getText().toString());
				data.putExtra("invoiceTypeName", mCurrentSelectDetail.getText());
				data.putExtra("invoiceType", (Integer) mCurrentSelectDetail.getTag());
				setResult(Define.ACTIVITY_CODE_SETUP_INVOICE, data);
				finish();
			}
			break;
			
		case R.id.tv_normal_invoice:
			if(mCurrentSelectType != mNormalInvoice) {
				mCurrentSelectType = mNormalInvoice;
				mNormalInvoice.setCompoundDrawables(getDrawable(R.drawable.ic_shopping_selected), null, null, null);
				mNotTrouble.setCompoundDrawables(getDrawable(R.drawable.ic_shopping_unselected), null, null, null);
			}
			break;
			
		case R.id.tv_not_trouble:
			if(mCurrentSelectType != mNotTrouble) {
				mCurrentSelectType = mNotTrouble;
				mNotTrouble.setCompoundDrawables(getDrawable(R.drawable.ic_shopping_selected), null, null, null);
				mNormalInvoice.setCompoundDrawables(getDrawable(R.drawable.ic_shopping_unselected), null, null, null);
			}
			break;
		}
	}

	@Override
	public void onLoadingCoverRefresh() {
		// TODO Auto-generated method stub
		ISmartMall.getInstance().getInvoiceType(mGetInvoiceTypeListener);
	}

	private void initListener() {
		// TODO Auto-generated method stub
		mGetInvoiceTypeListener = new GetInvoiceTypeListener() {
			
			@Override
			public void onResponseFailed() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed();
			}
			
			@Override
			public void onOffline() {
				// TODO Auto-generated method stub
				mOfflineDialog.show();
				mLoadingCover.onRefreshFailed(Define.MSG_OFFLINE_ERROR);
			}
			
			@Override
			public void onNoNetWork() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed(Define.MSG_NO_NETWORK);
			}
			
			@Override
			public void onNoMoreData() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onResponseSuccess(List<InvoiceTypeInfo> invoiceTypeInfos) {
				// TODO Auto-generated method stub
				initTypeLayout(invoiceTypeInfos);
				mLoadingCover.onRefreshSuccess();
			}
		};
	}
	
	private Drawable getDrawable(int resId) {
		Drawable d = getResources().getDrawable(resId);
		d.setBounds(0, 0, Tools.dip2px(getApplicationContext(), 21), 
				Tools.dip2px(getApplicationContext(), 21));
		return d;
	}
	
	
//	private void initInvoiceHead() {
//		if(mInvoiceInfo != null) {
//			mInvoiceHead.setText((CharSequence) mInvoiceInfo.get("invoice_top"));
//		}
//	}

	private void initTypeLayout(List<InvoiceTypeInfo> invoiceTypeInfos) {
		for(int i = 0; i < invoiceTypeInfos.size(); i++) {
			TextView view = (TextView) mInflater.inflate(R.layout.invoice_type_item, null);
			int resId = i == 0 ? R.drawable.ic_shopping_selected : R.drawable.ic_shopping_unselected;
			view.setCompoundDrawablePadding(Tools.dip2px(getApplicationContext(), 3));
			view.setCompoundDrawables(getDrawable(resId), null, null, null);
			view.setText(invoiceTypeInfos.get(i).getName());
			view.setTag(invoiceTypeInfos.get(i).getType());
			mTypeLayout.addView(view);
			((LinearLayout.LayoutParams) view.getLayoutParams()).topMargin = Tools.dip2px(getApplicationContext(), 10);
			view.setOnClickListener(this);
			
			if(i == 0) {
				mCurrentSelectDetail = view;
			}
		}
	}
}
